package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CharacterAnalysisActivity;
import flc.ast.activity.StarOverviewActivity;
import flc.ast.activity.StarQueryActivity;
import flc.ast.adapter.TodayIndexAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.dialog.SelStarDialog;
import stark.common.apis.ApiManager;
import stark.common.apis.base.ConstTodayBean;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private String starsName;//星座名称
    private TodayIndexAdapter todayIndexAdapter;
    private BasePopupView selPopup;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        //设置选择的星座名称 第一次进入时默认为白羊座
        starsName = SPUtil.getString(mContext, AppConfig.Name, getString(R.string.star_sign1));
        mDataBinding.tvStarSignChange.setText(starsName);
        mDataBinding.rvTodayList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        todayIndexAdapter = new TodayIndexAdapter();
        mDataBinding.rvTodayList.setAdapter(todayIndexAdapter);

        mDataBinding.tvStarSignChange.setOnClickListener(this);
        mDataBinding.llStarQuery.setOnClickListener(this);
        mDataBinding.llStarOverview.setOnClickListener(this);
        mDataBinding.llCharacterAnalysis.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvStarSignChange://选择星座
                selDialog();
                break;
            case R.id.llStarQuery://星座查询
                startActivity(StarQueryActivity.class);
                break;
            case R.id.llStarOverview://星座总览
                startActivity(StarOverviewActivity.class);
                break;
            case R.id.llCharacterAnalysis://性格分析
                CharacterAnalysisActivity.starsName = starsName;
                startActivity(CharacterAnalysisActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        getConstToday();
    }

    //获取今日势运
    private void getConstToday() {
        ApiManager.constApi().getConstToday(this, starsName, new IReqRetCallback<ConstTodayBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable ConstTodayBean result) {
                if (success) {
                    setTodayData(result);
                } else {
                    ToastUtils.showShort(getString(R.string.is_limit));
                }
            }
        });
        setTextAndImg();
    }

    //设置今日势运
    private void setTodayData(ConstTodayBean result) {
        mDataBinding.tvLove.setText(getString(R.string.love, result.getLove()));//爱情
        mDataBinding.tvWealth.setText(getString(R.string.wealth, result.getMoney()));//财运
        mDataBinding.tvCause.setText(getString(R.string.cause, result.getWork()));//事业
        mDataBinding.tvHealth.setText(getString(R.string.health, result.getHealth()));//健康
        int all = Integer.parseInt(result.getAll()) / 20;
        List<Boolean> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            if (i <= all) {
                list.add(new Boolean(true));
            } else {
                list.add(new Boolean(false));
            }
        }
        todayIndexAdapter.setList(list);
        mDataBinding.sbLove.setProgress(Integer.parseInt(result.getLove()));
        mDataBinding.sbWealth.setProgress(Integer.parseInt(result.getMoney()));
        mDataBinding.sbCause.setProgress(Integer.parseInt(result.getWork()));
        mDataBinding.sbHealth.setProgress(Integer.parseInt(result.getHealth()));
    }

    private void selDialog() {
        SelStarDialog selDialog = new SelStarDialog(mContext);
//                polygonDialog.setCurrentPos(mSelectPolygonPos);
//                polygonDialog.show();
        selPopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(mDataBinding.tvStarSignChange).hasShadowBg(false) // 去掉半透明背景
                .asCustom(selDialog);
        selPopup.show();
        selDialog.setListener(new SelStarDialog.Ilistener() {
            @Override
            public void onClick(String name) {
                starsName = name;
                SPUtil.putString(mContext, AppConfig.Name, starsName);
                mDataBinding.tvStarSignChange.setText(starsName);
                getConstToday();
            }
        });
    }

    private void setTextAndImg() {
        mDataBinding.tvSelStar.setText(starsName);
        if (starsName.equals(getString(R.string.star_sign1))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_byz);
        } else if (starsName.equals(getString(R.string.star_sign2))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_jnz);
        } else if (starsName.equals(getString(R.string.star_sign3))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_szz2);
        } else if (starsName.equals(getString(R.string.star_sign4))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_jxz);
        } else if (starsName.equals(getString(R.string.star_sign5))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_szz);
        } else if (starsName.equals(getString(R.string.star_sign6))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_cnz);
        } else if (starsName.equals(getString(R.string.star_sign7))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_tcz);
        } else if (starsName.equals(getString(R.string.star_sign8))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_txz);
        } else if (starsName.equals(getString(R.string.star_sign9))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_ssz);
        } else if (starsName.equals(getString(R.string.star_sign10))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_mjz);
        } else if (starsName.equals(getString(R.string.star_sign11))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_spz);
        } else if (starsName.equals(getString(R.string.star_sign12))) {
            mDataBinding.ivStarSign.setImageResource(R.drawable.aimg_xz_syz);
        }
    }

}